public class MyBinarySearchTree {
    public static class Node{
        int val;
        Node left;
        Node right;

        public Node(int val) {
            this.val = val;
        }
    }
    public Node root=null;
    public Node search(int key){
        if(root==null){
            return null;
        }
        Node cur=root;
        while(cur!=null){
            if(cur.val<key){
                cur=cur.right;
            }else if(cur.val>key){
                cur=cur.left;
            }else{
                return cur;
            }
        }
        return null;
    }
    public void insert(int key){
        if(root==null){
            root=new Node(key);
            return ;
        }
        Node head=new Node(key);
        Node cur=root;
        Node prev=cur;
        while(cur!=null){
            if(cur.val<head.val){
                prev=cur;
                cur=cur.right;
            }else{
                prev=cur;
                cur=cur.left;
            }
        }
        if(prev.val<head.val){
            prev.right=head;
        }else{
            prev.left=head;
        }
    }
    public Node remove(Node root,int key){
        if(root==null){
            return root;
        }
        if(root.val>key){
            root.left=remove(root.left,key);
        }else if(root.val<key){
            root.right=remove(root.right,key);
        }else{
            if(root.left==null){
                return root.right;
            }
            if(root.right==null){
                return root.left;
            }
            Node tmp=root.right;
            int min=tmp.val;
            while(tmp!=null){
                tmp=tmp.left;
                min=tmp.val;
            }
            root.val=min;
            root.right=remove(root.right,min);
        }
        return root;
    }
}
